package com.ssis.core;

import java.util.EnumMap;
import com.ssis.core.Cell.CellType;
import com.ssis.graphics.Sprite;

public class HUDStation {
	private Sprite background;
	private Sprite[] buttons;
	private Station station;
	private int x = 672;
	private int y = 0;
	public static final int BUTTON_WIDTH = 64;
	private static EnumMap<CellType, CellType[]> techTree;
	private static EnumMap<CellType, Integer> buttonFrames;
	
	public HUDStation(Station station) {
		this.station = station;
		background = new Sprite("hud_station_background", x, y, 0, 1, 4);
		buttons = new Sprite[16];
		for(int i=0; i<buttons.length; i++) {
			buttons[i] = new Sprite("hud_station_buttons", x + BUTTON_WIDTH * (i/8), y + BUTTON_WIDTH * (i%8), 0, 4, 4);
		}
		initTechTree();
		initButtonFrames();
	}
	
	public void render() {
		background.render();
		int i=0;
		while (i<techTree.get(station.selectedType()).length) {
			buttons[i].changeFrame(buttonFrames.get(techTree.get(station.selectedType())[i]));
			buttons[i].render();
			i++;
		}
		while (i<buttons.length) {
			buttons[i].changeFrame(buttonFrames.get(CellType.EMPTY));
			buttons[i].render();
			i++;
		}
		buttons[5].changeFrame(8);
		buttons[6].changeFrame(9);
		buttons[7].changeFrame(10);
		buttons[5].render();
		buttons[6].render();
		buttons[7].render();
	}
	
	public int click(int x, int y) {
		if(x < this.x || y < this.y)
			return 0;
		y -= this.y;
		x -= this.x;
		if(x < BUTTON_WIDTH) {
			return y / BUTTON_WIDTH + 1;
		} else {
			return y / BUTTON_WIDTH + 9;
		}
		//return 0;
	}
	
	public CellType getButtonType(int button){
		if (techTree.get(station.selectedType()).length > button)
			return techTree.get(station.selectedType())[button];
		return null;
	}
	
	private static void initTechTree() {
		techTree = new EnumMap<CellType, CellType[]>(CellType.class);
		techTree.put(CellType.EMPTY, new CellType[]{CellType.FRAME});
		techTree.put(CellType.FRAME, new CellType[]{CellType.CARGO, CellType.GENERATOR});
		techTree.put(CellType.CARGO, new CellType[]{CellType.DORM, CellType.CAPACITOR});
		techTree.put(CellType.DORM, new CellType[]{CellType.QUARTERS, CellType.OFFICE});
		techTree.put(CellType.QUARTERS, new CellType[]{});
		techTree.put(CellType.OFFICE, new CellType[]{});
		techTree.put(CellType.GENERATOR, new CellType[]{});
		techTree.put(CellType.CAPACITOR, new CellType[]{});
	}
	
	private static void initButtonFrames() {
		buttonFrames = new EnumMap<CellType, Integer>(CellType.class);
		buttonFrames.put(CellType.EMPTY, 0);
		buttonFrames.put(CellType.FRAME, 1);
		buttonFrames.put(CellType.CARGO, 2);
		buttonFrames.put(CellType.DORM, 3);
		buttonFrames.put(CellType.QUARTERS, 4);
		buttonFrames.put(CellType.OFFICE, 5);
		buttonFrames.put(CellType.GENERATOR, 6);
		buttonFrames.put(CellType.CAPACITOR, 7);
	}
}
